/*--------------------------------------------------------
Copyright (c) 2010 Apolo Pena

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
--------------------------------------------------------*/
package org.flashalchemy.ezlib.ezlog.data
{
    import org.flashalchemy.ezlib.ezlog.targets.EzTraceTarget;
    import mx.logging.ILogger;
    import mx.logging.Log;


    /**
     * <b>Description:</b> This class is used to validate strings related to the EzLogData .xml file.<p/>
     * <b>Usage:</b></br>
     * <pre>
     * 		var logDataValidator:EzLogDataValidator = new EzLogDataValidator();
     *      if (logDataValidator.isValidLogTargetData(logTargetPayload))
     *      {
     *      	logTargetPayloads.push(logTargetPayload);
     *		    }
     * </pre>
     * @see org.flashalchemy.ezlib.logging.data.EzLogDataXmlParser
     */
    public class EzLogDataValidator
    {
        /**
         * Valid log target string, for validating the EzLogData .xml
         * @default
         */
        public static const LOG_TARGET_TYPE_EZ_TRACE_TARGET:String = 'EzTraceTarget';

        /**
         * Valid EzProgramConsoleTarget string, for validating the EzLogData .xml
         * @default
         */
        public static const LOG_TARGET_TYPE_EZ_PROGRAM_CONSOLE_TARGET:String = 'EzProgramConsoleTarget';

        /**
         * Valid formatting type, for validating the EzLogData .xml
         * @default
         */
        public static const LOG_TARGET_FORMATTING_TYPE_DEFAULT:String = 'default';

        /**
         * Valid formatting type, for validating the EzLogData .xml
         * @default
         */
        public static const LOG_TARGET_FORMATTING_TYPE_VERBOSE:String = 'verbose';

        /**
         * Valid formatting type, for validating the EzLogData .xml
         * @default
         */
        public static const LOG_TARGET_FORMATTING_TYPE_MINIMAL:String = 'minimal';

        /**
         * Valid formatting type, for validating the EzLogData .xml
         * @default
         */
        public static const LOG_TARGET_FORMATTING_TYPE_BLANK:String = 'blank';

        /**
         * Valid formatting type, for validating the EzLogData .xml
         * @default
         */
        public static const LOG_TARGET_THEME_TYPE_DEFAULT:String = 'default';

        /**
         * Valid theme type, for validating the EzLogData .xml
         * @default
         */
        public static const LOG_TARGET_THEME_TYPE_MONOCHROMATIC:String = 'monochromatic';

        /**
         * Dedicated <code>mx.core.ILogger</code> used to ouput error message regarding this class.
         * Outside entities should not use this poroprty, rather they should use <code>org.flashalchemy.ezlib.logging.EzLog</code>
         * for all their logging needs.
         * @default Log.getLogger('org.flashalchemy.ezlib.logging.data.EzLogDataValidator')
         */
        public static var DEDICATED_LOGGER:ILogger = Log.getLogger('org.flashalchemy.ezlib.ezlog.data.EzLogDataValidator');

        /**
         * Array of valid log target type strings
         * @return Array of Strings
         */
        public static function get VALID_LOG_TARGET_TYPES():Array
        {
            return [ EzLogDataValidator.LOG_TARGET_TYPE_EZ_TRACE_TARGET, EzLogDataValidator.LOG_TARGET_TYPE_EZ_PROGRAM_CONSOLE_TARGET ];
        }

        /**
         * Array of valid log target formmating types
         * @return Array of Strings
         */
        public static function get VALID_LOG_TARGET_FORMATTING_TYPES():Array
        {
            return [ EzLogDataValidator.LOG_TARGET_FORMATTING_TYPE_DEFAULT, EzLogDataValidator.LOG_TARGET_FORMATTING_TYPE_VERBOSE, EzLogDataValidator.LOG_TARGET_FORMATTING_TYPE_MINIMAL, EzLogDataValidator.
                     LOG_TARGET_FORMATTING_TYPE_BLANK ]
        }

        /**
         * Array of valid log target theme strings
         * @return Array of Strings
         */
        public static function get VALID_LOG_TARGET_THEMES():Array
        {
            return [ EzLogDataValidator.LOG_TARGET_THEME_TYPE_DEFAULT, EzLogDataValidator.LOG_TARGET_THEME_TYPE_MONOCHROMATIC ];
        }

        /**
         * Constructor: does nothing
         */
        public function EzLogDataValidator()
        {
        }

        /**
         * Validates a log target type string
         * @param value
         * @return true on success and false on faliure
         */
        public function isValidLogTargetType(value:String):Boolean
        {
            var isValid:Boolean = false;
            var items:Array = EzLogDataValidator.VALID_LOG_TARGET_TYPES;
            var len:int = items.length;
            for (var i:int = 0; i < len; i++)
            {
                if (value == items[i])
                {
                    isValid = true;
                    break;
                }
            }
            if (!isValid)
            {
                EzLogDataValidator.DEDICATED_LOGGER.error('Invalid log target type id: ' + value + ' detected, likley culprit is the xml file used to initialize the EzLog');
            }
            return isValid;
        }

        /**
         * Validates an array of strings using <code>mx.logging.Log.hasIllegalCharacters()</code>
         * @param value
         * @return true on success and false on faliure
         * @see mx.logging.Log
         */
        public function isValidLogTargetFilter(value:Array):Boolean
        {
            var items:Array = value;
            var len:int = items.length;
            for (var i:int = 0; i < len; i++)
            {
                if (Log.hasIllegalCharacters(items[i]))
                {
                    EzLogDataValidator.DEDICATED_LOGGER.error("Illegal characters in log filter string: '" + items[i] + "' , likley culprit is the xml file used to initialize the EzLog.");
                    return false;
                }
            }
            return true;
        }

        /**
         * Validates a log target format string
         * @param value
         * @return true on success and false on faliure
         */
        public function isValidLogTargetFormat(value:String):Boolean
        {
            var isValid:Boolean = false;
            var items:Array = EzLogDataValidator.VALID_LOG_TARGET_FORMATTING_TYPES;
            var len:int = items.length;
            for (var i:int = 0; i < len; i++)
            {
                if (value == items[i] || value == null)
                {
                    isValid = true;
                    break;
                }
            }
            if (!isValid)
            {
                EzLogDataValidator.DEDICATED_LOGGER.error('Invalid log target format: ' + value + ', likley culprit is the xml file used to initialize the EzLog.');
            }
            return isValid;
        }

        /**
         * Validates a log target theme string
         * @param value
         * @return true on success and false on faliure
         */
        public function isValidLogTargetTheme(value:String):Boolean
        {
            if (value == null)
            {
                return true;
            }
            var isValid:Boolean = false;
            var items:Array = EzLogDataValidator.VALID_LOG_TARGET_THEMES;
            var len:int = items.length;
            for (var i:int = 0; i < len; i++)
            {
                if (value == items[i])
                {
                    isValid = true;
                    break;
                }
            }
            if (!isValid)
            {
                EzLogDataValidator.DEDICATED_LOGGER.error('Invalid log target themeId: ' + value + ' detected, likley culprit is the xml file used to initialize the EzLog.');
            }
            return isValid;
        }

        /**
         * Validates an <code>EzLogTargetData</code> object
         * @param eltd An EzLogTargetData object
         * @return true on success and false on faliure
         */
        public function isValidLogTargetData(eltd:EzLogTargetData):Boolean
        {
            if (isValidLogTargetType(eltd.targetType) && isValidLogTargetTheme(eltd.consoleThemeId) && isValidLogTargetFormat(eltd.formatting))
            {
                return true;
            }
            EzLogDataValidator.DEDICATED_LOGGER.error('Invalid LogTargetData object detected.');
            return false;
        }

        /**
         * Validates an <code>EzLogTargetFilterData</code> object
         * @param ltfd An EzLogFilterData object
         * @return true on success and false on faliure
         */
        public function isValidLogTargetFilterData(ltfd:EzLogTargetFilterData):Boolean
        {
            if (isValidLogTargetFilter(ltfd.filters))
            {
                return true;
            }
            EzLogDataValidator.DEDICATED_LOGGER.error('Invalid LogTargetFilterData object detected.');
            return false
        }

        /**
         * Validates an <code>EzLogData</code> object
         * @param logData an EzLogData object
         * @return true on success and false on faliure
         */
        public function isValidLogData(logData:EzLogData):Boolean
        {
            var isValid:Boolean;
            isValidLogTargetData(logData.logTargetData) && isValidLogTargetFilterData(logData.logTargetFilterData) ? isValid = true : isValid = false;
            if (!isValid)
            {
                EzLogDataValidator.DEDICATED_LOGGER.error('Invalid EzLogData detected, likley culprit is the xml file used to initialize the EzLog.');
            }
            return isValid;
        }
    }
}

